home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr49 / vesa18.zip / VESA_ET6.C < prev    next >
C/C++ Source or Header  |  1995-01-28  |  7KB  |  282 lines

  1. /* VESA package for emx/gcc --- Copyright (c) 1993 by Johannes Martin */
  2. #include <string.h>
  3. #include <sys/hw.h>
  4.  
  5. #define INCL_VIO
  6. #define INCL_DOSMEMMGR
  7. #define INCL_DOSFILEMGR
  8. #define INCL_DOSMISC
  9.  
  10. #include <os2emx.h>
  11. #include <os2thunk.h>
  12.  
  13. #include "vgaports.h"
  14. #include "vesa.h"
  15. #include "vesadll.h"
  16. #include "common.h"
  17.  
  18. /* VGA indexes max counts */
  19. #define CRT_C   24      /* 24 CRT Controller Registers */
  20. #define ATT_C   21      /* 21 Attribute Controller Registers */
  21. #define GRA_C   9       /* 9  Graphics Controller Registers */
  22. #define SEQ_C   5       /* 5  Sequencer Registers */
  23. #define MIS_C   1       /* 1  Misc Output Register */
  24. #define EXT_C   11      /* 10 SVGA Extended Registers */
  25. #define HIC_C   1       /* 1  Hicolor Register     */
  26.  
  27. /* VGA registers saving indexes */
  28. #define CRT     0               /* CRT Controller Registers start */
  29. #define ATT     CRT+CRT_C       /* Attribute Controller Registers start */
  30. #define GRA     ATT+ATT_C       /* Graphics Controller Registers start */
  31. #define SEQ     GRA+GRA_C       /* Sequencer Registers */
  32. #define MIS     SEQ+SEQ_C       /* General Registers */
  33. #define EXT     MIS+MIS_C       /* SVGA Extended Registers */
  34. #define HIC     EXT+EXT_C       /* Hicolor Register */
  35.  
  36. #define SEG_SELECT 0x3CD
  37.  
  38. #define NUM_REGS   72
  39.  
  40. VESAWORD *Modes                     = NULL;
  41. struct _ModeInfo *ModeInfos         = NULL;
  42. VESACHAR (*ModeRegisters)[NUM_REGS] = NULL;
  43.  
  44. VESABOOL Initialize(void)
  45. {
  46.   static VESAWORD NoModes = 0xFFFF;
  47.  
  48.   VESAWORD count, i;
  49.   CHAR     FileName[260];
  50.   HFILE    File;
  51.   ULONG    dummy;
  52.   PVOID    mem;
  53.  
  54.   _portaccess(ATT_IW, IS1_R);
  55.   Modes = &NoModes;
  56.   if (DosSearchPath(3, "DPATH", "VESAMODE.DAT", FileName, sizeof(FileName)) ||
  57.       DosOpen(FileName, &File, &dummy, 0, 0,
  58.               OPEN_ACTION_FAIL_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
  59.               OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READONLY, NULL))
  60.     {
  61.       Error("VESA.DLL: Cannot find VESAMODE.DAT.");
  62.       return(FALSE);
  63.     }
  64.   if (DosRead(File, &count, sizeof(count), &dummy) ||
  65.       (dummy != sizeof(count)) || (count == 0))
  66.     {
  67.       DosClose(File);
  68.       Error("VESA.DLL: Invalid VESAMODE.DAT.");
  69.       return(FALSE);
  70.     }
  71.   if (DosAllocMem(&mem, (count + 1) * sizeof(VESAWORD) +
  72.                         count * sizeof(struct _ModeInfo) +
  73.                         count * NUM_REGS * sizeof(VESACHAR),
  74.                   PAG_COMMIT | PAG_READ | PAG_WRITE))
  75.     {
  76.       DosClose(File);
  77.       Error("VESA.DLL: Not enough memory.");
  78.       return(FALSE);
  79.     }
  80.   ModeInfos     = mem;
  81.   Modes         = mem + count * sizeof(struct _ModeInfo);
  82.   ModeRegisters = mem + count * sizeof(struct _ModeInfo)
  83.                       + (count + 1) * sizeof(VESAWORD);
  84.   for (i = 0; i < count; i++)
  85.     {
  86.       if (DosRead(File, &Modes[i], sizeof(VESAWORD), &dummy)                    ||
  87.           (dummy != sizeof(VESAWORD))                                           ||
  88.           DosRead(File, &ModeInfos[i].Vesa, sizeof(VESAMODEINFO), &dummy)       ||
  89.           (dummy != sizeof(VESAMODEINFO))                                       ||
  90.           DosRead(File, &ModeRegisters[i], NUM_REGS * sizeof(VESACHAR), &dummy) ||
  91.           (dummy != NUM_REGS * sizeof(VESACHAR)))
  92.         {
  93.           DosClose(File);
  94.           DosFreeMem(mem);
  95.           Modes = &NoModes;
  96.           Error("VESA.DLL: Invalid VESAMODE.DAT.");
  97.           return(FALSE);
  98.         }
  99.       ModeInfos[i].fbtype = 0;
  100.       ModeInfos[i].Vesa.SetWindowFunc = OS2VesaSetWindow;
  101.       ModeInfos[i].Vesa.GetWindowFunc = OS2VesaGetWindow;
  102.     }
  103.   DosClose(File);
  104.   Modes[i] = 0xFFFF;
  105.   DosSetMem(mem, (count + 1) * sizeof(VESAWORD) +
  106.                  count * sizeof(struct _ModeInfo) +
  107.                  count * NUM_REGS * sizeof(VESACHAR), PAG_READ);
  108.   return(TRUE);
  109. }
  110.  
  111. VOID ClearUp(void)
  112. {
  113.   DosFreeMem(ModeInfos);
  114. }
  115.  
  116. VOID *GetPhysBuf(void)
  117. {
  118.   VIOMODEINFO ModeInfo;
  119.   VIOPHYSBUF  PhysBuf;
  120.  
  121.   ModeInfo.cb  = sizeof(ModeInfo);
  122.   VioGetMode(&ModeInfo, 0);
  123.   PhysBuf.pBuf = (PBYTE) ModeInfo.buf_addr;
  124.   PhysBuf.cb   = 0x10000;
  125.   if (VioGetPhysBuf(&PhysBuf, 0) != 0)
  126.     return(NULL);
  127.   else
  128.     return(MAKEP(PhysBuf.asel[0], 0));
  129. }
  130.  
  131. VESABOOL SetMode(VESAWORD mode)
  132. {
  133.   VIOMODEINFO ModeInfo;
  134.  
  135.   if (ModeInfos[mode].Vesa.ModeAttributes & 0x10)
  136.     {
  137.       ModeInfo.cb     = 12;
  138.       ModeInfo.fbType = 3;
  139.       ModeInfo.color  = 8;
  140.       ModeInfo.col    = 40;
  141.       ModeInfo.row    = 25;
  142.       ModeInfo.hres   = 320;
  143.       ModeInfo.vres   = 200;
  144.     }
  145.   else
  146.     {
  147.       ModeInfo.cb     = 8;
  148.       ModeInfo.fbType = 1;
  149.       ModeInfo.color  = 4;
  150.       ModeInfo.col    = 80;
  151.       ModeInfo.row    = 25;
  152.     }
  153.   return(VioSetMode(&ModeInfo, 0) == 0);
  154. }
  155.  
  156. static void hicolor_set_regs(char regs[])
  157. {
  158.   /* (re)set hicolor */
  159.  
  160.   _inp8(0x3c8);
  161.   _inp8(0x3c6);
  162.   _inp8(0x3c6);
  163.   _inp8(0x3c6);
  164.   _inp8(0x3c6);
  165.   _outp8(0x3c6, regs[HIC]);
  166.   _inp8(0x3c8);
  167. }
  168.  
  169. static void et4000_set_regs(char regs[])
  170. {
  171.   int i;
  172.  
  173.   /* write some ET4000 specific registers */
  174.   _outp8(0x3c3, regs[EXT+7]);
  175.   _outp8(0x3cd, regs[EXT+8]);
  176.  
  177.   /* write extended sequencer register */
  178.   _outp8(SEQ_I, 7);
  179.   _outp8(SEQ_D, regs[EXT+6]);
  180.  
  181.   /* write extended CRT registers */
  182.   for (i = 0; i < 6; i++)
  183.     {
  184.       _outp8(CRT_I, 0x32+i);
  185.       _outp8(CRT_D, regs[EXT+i]);
  186.     }
  187.  
  188.   /* write extended attribute register */
  189.   _inp8(IS1_R);    /* reset flip flop */
  190.   _outp8(ATT_IW, 0x16);
  191.   _outp8(ATT_IW, regs[EXT+9]);
  192.   /* write another ET4000 CRT register */
  193.   _outp8(CRT_I, 0x3f);
  194.   _outp8(CRT_D, regs[EXT+10]);
  195. }
  196.  
  197. static void set_regs(char regs[])
  198. {
  199.   int i;
  200.  
  201.   /* update misc output register */
  202.   _outp8(MIS_W, regs[MIS]);
  203.  
  204.   /* synchronous reset on */
  205.   _outp8(SEQ_I, 0x00);
  206.   _outp8(SEQ_D, 0x01);
  207.  
  208.   /* write sequencer registers */
  209.   for (i = 1; i < SEQ_C; i++)
  210.     {
  211.       _outp8(SEQ_I, i);
  212.       _outp8(SEQ_D, regs[SEQ+i]);
  213.     }
  214.  
  215.   /* synchronous reset off */
  216.   _outp8(SEQ_I, 0x00);
  217.   _outp8(SEQ_D, 0x03);
  218.  
  219.   /* deprotect CRT registers 0-7 */
  220.   _outp8(CRT_I, 0x11);
  221.   _outp8(CRT_D, _inp8(CRT_D)&0x7F);
  222.  
  223.   /* write CRT registers */
  224.   for (i = 0; i < CRT_C; i++)
  225.     {
  226.       _outp8(CRT_I, i);
  227.       _outp8(CRT_D, regs[CRT+i]);
  228.     }
  229.  
  230.   /* write graphics controller registers */
  231.   for (i = 0; i < GRA_C; i++)
  232.     {
  233.       _outp8(GRA_I, i);
  234.       _outp8(GRA_D, regs[GRA+i]);
  235.     }
  236.  
  237.   /* write attribute controller registers */
  238.   for (i = 0; i < ATT_C; i++)
  239.     {
  240.       _inp8(IS1_R);   /* reset flip-flop */
  241.       _outp8(ATT_IW, i);
  242.       _outp8(ATT_IW, regs[ATT+i]);
  243.     }
  244. }
  245.  
  246. void SetSpecial(VESAWORD mode)
  247. {
  248.   /* disable video */
  249.   _inp8(IS1_R);
  250.   _outp8(ATT_IW, 0x00);
  251.  
  252.   /* unlock extended registers */
  253.   _outp8(0x3bf, 3);
  254.   _outp8(0x3d8, 0xa0);
  255.  
  256.   set_regs(ModeRegisters[mode]);
  257.   et4000_set_regs(ModeRegisters[mode]);
  258.   hicolor_set_regs(ModeRegisters[mode]);
  259.  
  260.   /* enable video */
  261.   _inp8(IS1_R);
  262.   _outp8(ATT_IW, 0x20);
  263. }
  264.  
  265. VESABOOL OS2VesaSetWindow(VESACHAR Window, VESAWORD Address)
  266. {
  267.   if (Window == 0)
  268.     _outp8(SEG_SELECT, (_inp8(SEG_SELECT) & 0xF0) | Address);
  269.   else
  270.     _outp8(SEG_SELECT, (_inp8(SEG_SELECT) & 0x0F) | (Address << 4));
  271.   return(TRUE);
  272. }
  273.  
  274. VESABOOL OS2VesaGetWindow(VESACHAR Window, PVESAWORD Address)
  275. {
  276.   if (Window == 0)
  277.     *Address = _inp8(SEG_SELECT) & 0x0F;
  278.   else
  279.     *Address = _inp8(SEG_SELECT) >> 4;
  280.   return(TRUE);
  281. }
  282.